home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS02.ADF
/
Asm
/
trees.doc
< prev
next >
Wrap
Text File
|
1989-05-30
|
4KB
|
77 lines
Documentation for the Unix System V functions tsearch(), tdelete(), and
twalk() ported to the Commodore Amiga. Object is in the file trees.o and
can be linked with a C or assembler program.
This document is formatted in a style similar to the Unix System V programmers
manual.
Name:
tsearch, tdelete, twalk - manage binary trees.
Synopsis:
extern char *tsearch((char *) key, (char **)rootpntr, compar)
int (*compar) ();
extern char *tdelete((char *) key, (char **)rootpntr, compar)
int (*compar)();
extern void twalk((char *)rootp,action)
void (*action)();
Description:
tsearch is a binary tree search routine. It returns a pointer into a tree
indicating where a datum may be found. If the datum does not occur in the
tree it is added at an appropriate point in the tree.
Argument description: KEY points to the datum to be sought in the tree.
ROOTPNTR points to a variable that points to the root of the tree (Notice
the double indirection here.) A NULL pointer value for the variable
denotes an empty tree; in this case, the variable will be set to point to
the datum at the root of the new tree. Compar is the name of the user
provided comparison function. It is called with two arguments that point
to the elements being compared. The function must return an integer less
than, equal to, or greater than zero signifying that the first argument is
less than, equal to, or greater than the second. Compar need not compare
every byte in each argument so that the arguments may be structures
containing arbitrary data in addition to the key itself. (See qtest.c for
an example of how to write a comparison function.)
tdelete deletes a node from a binary tree. The arguments are the same as
for tsearch. The variable pointed to by the ROOTPNTR will be changed if
the deleted node was the root of the tree. tdelete returns a pointer to
the parent of the deleted node, or a NULL pointer if the node is not found.
twalk traverses a binary search tree. ROOTP is the root of the tree to
be traversed. (Any node in a tree may be used as the root for a walk
below that node.) ACTION is the name of a routine to be invoked at each
node. This routine is, in turn, called with three arguments. The first
argument is the address of the node being visited. The second argument
is a value from an enumeration data type
typedef enum {preorder, postorder, endorder, leaf} VISIT;
depending on whether this is the first, second, or third time that the
node has been visited (during a depth-first, left-to-right traversal of
the tree), or whether the node is a leaf. (If enumeration types are not
available, use an int as the argument type and the values will be
respectively {0,1,2,3}. The third argument to ACTION is the level of
the node in the tree, with the root node being level 0.
** ACTION should be declared extern before it is defined to ensure proper
ordering of the arguments passed to it. This is needed for Lattice C
on the Amiga which expects the arguments in left to right order if the
function is not declared external. twalk passes them in right to left
order so ACTION must be declared extern even if it is present in the
same source file as the routine that calls twalk.
Notes:
Warning: the ROOTP argument to twalk() is one level of indirection
less than the ROOTPNTR arguments to tsearch and tdelete.
DIAGNOSTICS:
A NULL pointer is returned by tsearch if there is not enough memory
available to create a new node.
A NULL pointer is returned by tsearch and tdelete if ROOTPNTR is NULL
on entry.
BUGS
Awful things will happen if the calling function alters the pointer to
the root of a tree.